package com.limox.bishe.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.OnMenuVisibilityListener;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
 
 
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.Time;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.limox.bishe.R;
import com.limox.bishe.viewgroup.kbView;
 
public class kbActivity extends Activity {
	private List<View> mListViews;
    private TextView imageView;    
    private TextView[] imageViews;   
    private ViewGroup group;  
    private int currentIndex;   
    
	public  String[] titulosPaginas = { "周一", "周二" ,"周三","周四","周五","周六","周日"};

	
	@Override
	public  void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
 
		setContentView(R.layout.main);
		    
		mListViews=new ArrayList<View>();
		
		kbView kbview1=new kbView(kbActivity.this,"一");
		kbView kbview2=new kbView(kbActivity.this,"二");
		kbView kbview3=new kbView(kbActivity.this,"三");
		kbView kbview4=new kbView(kbActivity.this,"四");
		kbView kbview5=new kbView(kbActivity.this,"五");
		kbView kbview6=new kbView(kbActivity.this,"六");
		kbView kbview7=new kbView(kbActivity.this,"日");
	 
	  
 		mListViews.add(kbview1.getView()); 
		mListViews.add(kbview2.getView());
		mListViews.add(kbview3.getView());
		mListViews.add(kbview4.getView());
		mListViews.add(kbview5.getView());
		mListViews.add(kbview6.getView());
		mListViews.add(kbview7.getView());
 
		 
		
		final ViewPager pager=(ViewPager)findViewById(R.id.testViewPager); 
		pager.setAdapter(new MyPagerAdapter(mListViews));
		//根据时间选择默认页面
		// 根据周几选择默
		Time time = new Time();
		time.setToNow();  
		int currentDay;
		if(time.weekDay==0){
			currentDay=6;
		}else{
			currentDay=time.weekDay;
		}
		
		pager.setCurrentItem(currentDay-1);
		 
	  
		//设置日期指示器
		imageViews = new TextView[mListViews.size()];
		group = (ViewGroup)findViewById(R.id.viewGroup);
		 
		//将小圆点放到imageView数组当中   
	       for (int i = 0; i < mListViews.size(); i++) {    
	           imageView = new TextView(kbActivity.this);    
	           imageView.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,1.0f));  
	           imageView.setGravity(Gravity.CENTER);
  	           imageViews[i] = imageView;   
  	           
               imageViews[i].setText(titulosPaginas[i].substring(1,2));    
        	   imageViews[i].setBackgroundResource(R.drawable.daytitle_nochoice_bg);
 
        	   imageViews[i].setTextColor(0xff858585);
        	   
	           if (i ==currentDay-1) {   
	        	   imageViews[i].setBackgroundResource(R.drawable.daytitle_bg);
	        	   imageViews[i].setTextColor(0xff000000);

	            //默认选中第一张图片  
	           }  
	           
	           final int curritem=i;
	           
	           imageViews[i].setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					pager.setCurrentItem(curritem);

				}
			});
	           
	           group.addView(imageViews[i]);    
	       }    
	       
	       pager.setOnPageChangeListener(new GuidePageChangeListener());  
	         
	       
	       //设置Actionbar
	       ActionBar actionbar=getActionBar();
 	       Resources r=getResources();
 	       Drawable myDrawable=r.getDrawable(R.drawable.topbg);
 	       actionbar.setBackgroundDrawable(myDrawable);
	     
	       
	}
	
	
  
	/**
     * ViewPager适配器
*/
    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        
        
        @Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return titulosPaginas[position];
		}

		@Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

    // 指引页面更改事件监听器,左右滑动图片时候，小圆点变换显示当前图片位置  
    class GuidePageChangeListener implements OnPageChangeListener {    
         
        @Override    
        public void onPageScrollStateChanged(int arg0) {    
            // TODO Auto-generated method stub    
    
        }    
    
        @Override    
        public void onPageScrolled(int arg0, float arg1, int arg2) {    
            // TODO Auto-generated method stub    
    
        }    
    
        @Override    
        public void onPageSelected(int arg0) {    
         currentIndex = arg0;  
            for (int i = 0; i < imageViews.length; i++) {    
                imageViews[arg0].setBackgroundResource(R.drawable.daytitle_bg);
         	   imageViews[arg0].setTextColor(0xff000000);

                if (arg0 != i) {   
             	   imageViews[i].setTextColor(0xff858585); 
                    imageViews[i].setBackgroundResource(R.drawable.daytitle_nochoice_bg);    
                }    
            }  
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater=getMenuInflater();
		inflater.inflate(R.menu.main,menu);
		
		return true;
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_settings:
			//goto the setting activity
			Intent gointent=new Intent();
			gointent.setClass(kbActivity.this,Setting.class);
			startActivity(gointent);
			
			return true;
		default:
			return super.onOptionsItemSelected(item);
 		}
		 
		
	}

	
	 
    
    
}
